import $service from '../../../../service.js';
var that;
var page = 1;
const app = getApp();
Page({
    data: {
        active : true,
        prescriptionlist:[],
        num:0,
        num2:0,
    },
    onShow() {
        that =this.data;
        this.getOpenPrescriptions();
    },
    onPullDownRefresh() {
        page = 1;
        this.setData({
            loadingText: '加载更多'
        })
        this.getOpenPrescriptions();
        wx.stopPullDownRefresh()
    },
    onReachBottom: function() { 
        if (that.loadingType == true) {
            page++
            this.getOpenPrescriptions();
        } else {
            this.setData({
                loadingText: '没有更多了'
            })
        }
    },
    loading(){
        if (that.loadingType == true) {
            page++
            this.getOpenPrescriptions();
        } else {
            this.setData({
                loadingText: '没有更多了'
            })
        }
    },
    getOpenPrescriptions() {
        let params = {
            "current": page,
            "endDate": "",
            "patientName": "",
            "prescriptionState": that.active ? '1' : '2',
            "prescriptionType": "",
            "size": 10,
            "startDate": ""
          }
        app.http.getOpenPrescriptions(params).then(res => {
            let list = res.data.list;
            if (page == 1) {
                that.loadingText = '';
                that.prescriptionlist = list;
            } else {
                if (list.length <= 0) {
                    that.loadingType = false;
                    that.loadingText = '没有更多了';
                } else {
                    for (var i = 0; i < list.length; i++) {
                        that.prescriptionlist.push(list[i]);
                        that.loadingText = '加载更多';
                    }
                }
            }
            this.setData({
                prescriptionlist: that.prescriptionlist,
                loadingText:that.loadingText,
                loadingType:that.loadingType
            })
        }).catch(err => {
            $service.showToast(err.msg)
        })
    },
    tapactive(e){
        var index = e.currentTarget.dataset.index;
        if(index == 1){
            this.data.active = true
        }else{
            this.data.active = false
        }
        this.setData({
            active : this.data.active
        })
        this.getOpenPrescriptions();
    },
   gotopage(e){
       let {num, index} = e.currentTarget.dataset;
       if (index == 5) {
            $service.openPage('../../../user_side/index/index_health_guidance/index_health_guidance?id=' + this.data.prescriptionlist[num].id + '&type=4')
       } else {
            $service.openPage('/pages/doctor_side/index/index_edit_prescription/index?id=' + this.data.prescriptionlist[num].id) 
       }
   }
})
